//
//  YWNoQrcodeLedgerLocationTableViewCell.m
//  maintain
//
//  Created by 烟雨云渊 on 16/12/5.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWNoQrcodeLedgerLocationTableViewCell.h"
#import "YWMacro.h"
#import "Masonry.h"
#import "UIColor+CustomColors.h"
//#import "UILabel+AttributedText.h"
#import "UIImageView+WebCache.h"

#import "YWEquipment.h"
#import "YWDBTool.h"
#import "YWBuilding.h"
#import "YWBuildingManager.h"
#import "YWCommonTool.h"
#import "YWNoQrcodeLocationCellModel.h"
#import "YWApartment.h"
#import "YWFloor.h"
#import "YWLocation.h"
#import "YYLabel.h"

@interface YWNoQrcodeLedgerLocationTableViewCell()
/*楼盘图片*/
@property (nonatomic, strong) UIImageView* buildingImageView;
/*楼盘*/
@property (nonatomic, strong) YYLabel *buildingLabel;
/*地址图片*/
//@property (nonatomic, strong) UIImageView *locationIV;
/*地址*/
@property (nonatomic, strong) YYLabel *locationLabel;
/*设备总数*/
@property (nonatomic, strong) YYLabel * equipmentCount;
/*二维码设备数*/
@property (nonatomic, strong) YYLabel *qrcoreEquipment;

@property (nonatomic, strong) UIView* footLine;
@property (nonatomic, strong) UIView* marginLine1;
@property (nonatomic, strong) UIView* marginLine2;
//报障
@property (nonatomic, strong) UIButton* postTrouble;
//维保
@property (nonatomic, strong) UIButton* maintanceButton;
//详情
@property (nonatomic, strong) UIButton* detailButton;
@end

@implementation YWNoQrcodeLedgerLocationTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.layer.shouldRasterize = YES;
        self.layer.rasterizationScale = [UIScreen mainScreen].scale;
        [self setUpSubviews];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)setLocationModel:(YWNoQrcodeLocationCellModel *)locationModel{
    _locationModel = locationModel;
    //楼盘名
    NSString* downloadUrl = [YWCommonTool pictureDownloadUrl:_locationModel.building.surfacePicture];
    [self.buildingImageView sd_setImageWithURL:[NSURL URLWithString:downloadUrl] placeholderImage:[UIImage imageNamed:@"placeholder_buliding"]];
    self.buildingLabel.text = _locationModel.building.buildingName.length > 0 ? _locationModel.building.buildingName : @"未知楼盘";
    NSString* position = nil;
    if (locationModel.location.locationId>0 && locationModel.apartment.apartmentId==0) {
        position = [[YWBuildingManager shareManager] areaLocationWithLocationId:locationModel.location.locationId];
    }else{
        position = [NSString stringWithFormat:@"%@%@%@",_locationModel.apartment.apartmentId>0?_locationModel.apartment.apartmentName:@"",_locationModel.floor.floorId>0?_locationModel.floor.floorName:@"",_locationModel.location.locationId>0?_locationModel.location.locationName:@""];
    }
    NSString *locationStr = position.length ? position : self.buildingLabel.text;
    self.locationLabel.text = locationStr;
    
    NSString*equipmentCount = [NSString stringWithFormat:@"%ld",(long)_locationModel.equipmentCount];
    _equipmentCount.attributedText = [YWCommonTool attributedTextWithFirstText:@"设备数: " firstColor:[UIColor customTextGrayColor] secondText:equipmentCount secondColor:[UIColor customYellowColor] font:KSmallContentFont];
    
    NSString*qrcodeEquipments = [NSString stringWithFormat:@"%ld",(long)_locationModel.qrcodeEquipments];
    _qrcoreEquipment.attributedText = [YWCommonTool attributedTextWithFirstText:@"二维码数: " firstColor:[UIColor customTextGrayColor] secondText:qrcodeEquipments secondColor:[UIColor customYellowColor] font:KSmallContentFont];
    
    self.buildingLabel.preferredMaxLayoutWidth = self.buildingLabel.frame.size.width;
    self.locationLabel.preferredMaxLayoutWidth = self.locationLabel.frame.size.width;
}

- (void)setUpSubviews{
    [self.buildingImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView).offset(10);
        make.left.mas_equalTo(self.contentView).offset(10);
        make.size.mas_equalTo(CGSizeMake(kLabelH*2, kLabelH*2));
    }];
    
    [self.buildingLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.buildingImageView.mas_right).offset(10);
        make.right.mas_equalTo(self.contentView).offset(-10);
        make.centerY.mas_equalTo(self.buildingImageView.mas_centerY);
    }];
    
//    [self.locationIV mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(self.buildingLabel.mas_bottom).offset(10);
//        make.left.mas_equalTo(self.buildingImageView.mas_right).offset(10);
//        make.size.mas_equalTo(CGSizeMake(17, 17));
//    }];
    
    [self.locationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.buildingImageView.mas_bottom).offset(10);
        make.left.mas_equalTo(self.contentView.mas_left).offset(10);
        make.right.mas_equalTo(self.contentView).offset(-10);
    }];
    
    [self.equipmentCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.locationLabel.mas_bottom).offset(10);
        make.left.mas_equalTo(self.contentView.mas_left).offset(10);
        make.width.mas_equalTo(SCREEN_WIDTH/2);
//        make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-10);
    }];
    
    [self.qrcoreEquipment mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.locationLabel.mas_bottom).offset(10);
        make.right.mas_equalTo(self.contentView).offset(-10);
        make.left.mas_equalTo(self.equipmentCount.mas_right).offset(10);
//        make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-10);
    }];
    
    [self.footLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.equipmentCount.mas_bottom).offset(10);
        make.height.mas_equalTo(@1.0);
        make.width.mas_equalTo(SCREEN_WIDTH);
    }];
    
    [self.postTrouble mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.footLine.mas_bottom).offset(1);
        make.left.mas_equalTo(self.contentView);
        make.width.mas_equalTo(SCREEN_WIDTH/3-1);
        make.bottom.mas_equalTo(self.contentView).offset(-5);
    }];
    
    [self.marginLine1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.postTrouble);
        make.left.mas_equalTo(self.postTrouble.mas_right);
        make.width.mas_equalTo(@1.0);
        make.bottom.mas_equalTo(self.postTrouble);
    }];
    
    [self.maintanceButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.postTrouble);
        make.left.mas_equalTo(self.marginLine1.mas_right).offset(1);
        make.width.mas_equalTo(SCREEN_WIDTH/3-1);
        make.bottom.mas_equalTo(self.postTrouble);
    }];
    
    [self.marginLine2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.postTrouble);
        make.left.mas_equalTo(self.maintanceButton.mas_right);
        make.width.mas_equalTo(@1.0);
        make.bottom.mas_equalTo(self.postTrouble);
    }];
    
    [self.detailButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.postTrouble);
        make.left.mas_equalTo(self.marginLine2.mas_right).offset(1);
        make.right.mas_equalTo(self.contentView);
        make.bottom.mas_equalTo(self.postTrouble);
    }];
}

- (UIImageView*)buildingImageView
{
    if (!_buildingImageView) {
        UIImageView* buildingImageView = [[UIImageView alloc] init];
        buildingImageView.clipsToBounds = YES;
        buildingImageView.layer.cornerRadius = 22.0;
        _buildingImageView = buildingImageView;
        [self.contentView addSubview:_buildingImageView];
    }
    return _buildingImageView;
}
- (YYLabel*)buildingLabel
{
    if (!_buildingLabel) {
        YYLabel* buildingLabel = [[YYLabel alloc] init];
        buildingLabel.font = [UIFont boldSystemFontOfSize:16];
        buildingLabel.numberOfLines = 0;
        _buildingLabel = buildingLabel;
        [self.contentView addSubview:_buildingLabel];
    }
    return _buildingLabel;
}

//- (UIImageView*)locationIV
//{
//    if (!_locationIV) {
//        UIImageView* locationImageV = [[UIImageView alloc] init];
//        locationImageV.image = [UIImage imageNamed:@"common_location"];
//        _locationIV = locationImageV;
//        [self.contentView addSubview:_locationIV];
//    }
//    return _locationIV;
//}

- (YYLabel*)locationLabel
{
    if (!_locationLabel) {
        YYLabel* locationLabel = [[YYLabel alloc] init];
        locationLabel.font = KSmallContentFont;
        locationLabel.textColor = [UIColor customTextGrayColor];
        locationLabel.numberOfLines = 0;
        _locationLabel = locationLabel;
        [self.contentView addSubview:_locationLabel];
    }
    return _locationLabel;
}

- (YYLabel*)equipmentCount
{
    if (!_equipmentCount) {
        YYLabel* equipmentCount = [[YYLabel alloc] init];
        _equipmentCount = equipmentCount;
        [self.contentView addSubview:_equipmentCount];
    }
    return _equipmentCount;
}

- (YYLabel*)qrcoreEquipment{
    if (!_qrcoreEquipment) {
        YYLabel* qrcoreEquipment = [[YYLabel alloc] init];
        _qrcoreEquipment = qrcoreEquipment;
        [self.contentView addSubview:_qrcoreEquipment];
    }
    return _qrcoreEquipment;
}

-(UIButton *)postTrouble{
    if (!_postTrouble) {
        UIButton* postTrouble = [UIButton buttonWithType:UIButtonTypeSystem];
        [postTrouble setBackgroundColor:[UIColor whiteColor]];
        [postTrouble setTitle:@"报障" forState:UIControlStateNormal];
        postTrouble.titleLabel.font = kContentFont;
        [postTrouble setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [postTrouble addTarget:self action:@selector(handleButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
        postTrouble.tag = 1001;
        [self.contentView addSubview:postTrouble];
        _postTrouble = postTrouble;
    }
    return  _postTrouble;
}

-(UIButton *)maintanceButton{
    if (!_maintanceButton) {
        UIButton* maintanceButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [maintanceButton setBackgroundColor:[UIColor whiteColor]];
        [maintanceButton setTitle:@"维保" forState:UIControlStateNormal];
        maintanceButton.titleLabel.font = kContentFont;
        [maintanceButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [maintanceButton addTarget:self action:@selector(handleButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
        maintanceButton.tag = 1002;
        [self.contentView addSubview:maintanceButton];
        _maintanceButton = maintanceButton;
    }
    return _maintanceButton;
}

-(UIButton *)detailButton{
    if (!_detailButton) {
        UIButton* recordButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [recordButton setBackgroundColor:[UIColor whiteColor]];
        [recordButton setTitle:@"详情" forState:UIControlStateNormal];
        recordButton.titleLabel.font = kContentFont;
        [recordButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [recordButton addTarget:self action:@selector(handleButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
        recordButton.tag = 1003;
        [self.contentView addSubview:recordButton];
        _detailButton = recordButton;
    }
    return _detailButton;
}

-(UIView *)footLine{
    if (!_footLine) {
        UIView *footLine = [[UIView alloc]init];
        [footLine setBackgroundColor:[UIColor customLineColor]];
        [self.contentView addSubview:footLine];
        _footLine = footLine;
    }
    return _footLine;
}
-(UIView *)marginLine1{
    if (!_marginLine1) {
        UIView *marginLine1 = [[UIView alloc]init];
        [marginLine1 setBackgroundColor:[UIColor customLineColor]];
        [self.contentView addSubview:marginLine1];
        _marginLine1 = marginLine1;
    }
    return _marginLine1;
}
-(UIView *)marginLine2{
    if (!_marginLine2) {
        UIView *marginLine2 = [[UIView alloc]init];
        [marginLine2 setBackgroundColor:[UIColor customLineColor]];
        [self.contentView addSubview:marginLine2];
        _marginLine2 = marginLine2;
    }
    return _marginLine2;
}

- (void)handleButtonEvent:(UIButton*)sender
{
    if (sender.tag == 1001) {
        if ([self.delegate respondsToSelector:@selector(clickLocationPostTroubleButtonEvent:)]) {
            [self.delegate clickLocationPostTroubleButtonEvent:self];
        }
    }else if (sender.tag == 1002) {
        if ([self.delegate respondsToSelector:@selector(clickLocationMaintanceButtonEvent:)]) {
            [self.delegate clickLocationMaintanceButtonEvent:self];
        }
    }else if (sender.tag == 1003) {
        if ([self.delegate respondsToSelector:@selector(clickLocationDetailButtonEvent:)]) {
            [self.delegate clickLocationDetailButtonEvent:self];
        }
    }
}

@end
